#include "ble_send_task.h"
#include "ble_receive_task.h"
#include "tcp_client.h"
#include "device_app.h"

#include <stdio.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_uart.h"
#include "wifiiot_watchdog.h"

#include "terminal_send_task.h"

//1、关闭指定设备电源
//2、打开指定设备电源
//3、定时打开和关闭指定设备电源
//4、获取设备用电功率，超功率报警

#define SLAVE_OFF_LINE_TIME_MAX   150    //15s
#define BLE_RESTORE_TIME_MAX  20
#define BLE_CTRL_TIME_MAX  10

ble_send_data_t ble_send_data;
extern ble_receive_data_t ble_receive_data;
extern server_data_t server_data;
extern device_data_t device_data;
extern terminal_send_data_t terminal_send_data;


static void ble_restore(void);
static void ble_access(void);
static void ble_send_power_off(void);
static void ble_send_power_on(void);
static void ble_send_time(void);
static void ble_send_connect_replay(void);

static void ble_send_timing_off(void);
static void ble_send_timing_on(void);
static void ble_send_time_off(void);
static void ble_send_time_on(void);

void ble_send_task(void *arg)
{
    (void) arg;
    ble_send_task_init();
    while(1)
    {
        if(ble_send_data.ble_send_cmd_time == 1)
        {
            ble_send_data.ble_send_cmd_time = 0;
            ble_send_time();
        }
        if(ble_send_data.ble_send_cmd_connect_replay == 1)
        {
            ble_send_data.ble_send_cmd_connect_replay = 0;
            ble_send_connect_replay();//入网成功、从机连接成功都会发送一次0xA0.
        }
        switch(ble_send_data.ble_send_cmd_num)
        {
            case BLE_RESTORE        : ble_restore(); break;
            case BLE_CONNECT        : ble_access(); break;
            case BLE_SEND_POWER_OFF : ble_send_power_off(); break;
            case BLE_SEND_POWER_ON  : ble_send_power_on(); break;
            case BLE_SEND_RELAY_STATE : break;
            case BLE_SEND_TIMING_OFF : ble_send_timing_off(); break;//计时关任务
            case BLE_SEND_TIMING_ON : ble_send_timing_on(); break;//计时开任务
            case BLE_SEND_TIME_OFF : ble_send_time_off(); break;//计时关任务
            case BLE_SEND_TIME_ON  : ble_send_time_on(); break;//计时开任务

            default : break;
        }
        for(uint8_t i=0; i<ble_receive_data.ble_num; i++)
        {
            ble_receive_data.ble_slave_id_data[i].ble_slave_time++;
            if(ble_receive_data.ble_slave_id_data[i].ble_slave_time == SLAVE_OFF_LINE_TIME_MAX)
            {
                ble_receive_data.ble_slave_id_data[i].ble_slave_state = OFF_LINE;    //超过等待最长时间没有收到心跳包，判定设备离线。
                //当有设备连接状态被改变时，会将改变的状态发送给服务器。
            }
        }
        //在这里记录在线设备未发送心跳包时间。超过未发送心跳包的最大时间，判定设备离线。
        osDelay(10);  //100ms
    }
}

void ble_send_task_creat(void)
{
    osThreadAttr_t attr;

    attr.name       = "ble_send_task";
    attr.attr_bits  = 0U;
    attr.cb_mem     = NULL;
    attr.cb_size    = 0U;
    attr.stack_mem  = NULL;
    attr.stack_size = 5120;
    attr.priority   = osPriorityNormal1;

    if (osThreadNew(ble_send_task, NULL, &attr) == NULL)
    {
        printf("[ble_send_task_creat] Falied to create ble_send_task!\n");
    }
}

APP_FEATURE_INIT(ble_send_task_creat);


void ble_send_task_init(void)
{
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
    ble_send_data.ble_send_user_cmd_state_last = NOT_CMD;
    ble_send_data.ble_send_user_cmd_state = NOT_CMD;
    memset(ble_send_data.slave_id, 0, sizeof(ble_send_data.slave_id));
}

static void ble_restore(void)
{
    if(ble_receive_data.ble_state != RESTORING)
    {
        ble_receive_data.ble_state = RESTORING;
        uint8_t reset_data[3] = {0x02, 0xc0, 0x15};
        UartWrite(WIFI_IOT_UART_IDX_2, reset_data, sizeof(reset_data));    //发送控制命令
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_receive_data.ble_state == RESTORING)
	{
		if(ble_receive_data.ble_time_out < BLE_RESTORE_TIME_MAX)
		{
			ble_receive_data.ble_time_out++;
		}
		else
		{
			ble_receive_data.ble_state_last = ble_receive_data.ble_state;
			ble_receive_data.ble_state = RESTORE_TIMEOUT;
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
		}
	}
}

static void ble_access(void)
{
    if(ble_receive_data.ble_state != CONNECTING)
    {
        ble_receive_data.ble_state = CONNECTING;
        uint8_t access_data[3] = {0x02, 0xc0, 0x09};
        UartWrite(WIFI_IOT_UART_IDX_2, access_data, sizeof(access_data));
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_receive_data.ble_state == CONNECTING)
    {
		if(ble_receive_data.ble_time_out < 200)
		{
			ble_receive_data.ble_time_out++;
		}
        else
        {
            ble_receive_data.ble_state_last = ble_receive_data.ble_state;
			ble_receive_data.ble_state = CONNECT_TIMEOUT;
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
        }
    }
}

/***************************************************************************************************************************************
 *                                                               电源控制
 * *************************************************************************************************************************************/
static void ble_send_power_off(void)//需要带发送给哪个设备的参数，那就要在oled_task就要显示那些设备正常连接，再选择发送给谁，
{
    if(ble_send_data.ble_send_user_cmd_state != CMD_ING)
    {
        ble_send_data.ble_send_user_cmd_state = CMD_ING;
        uint8_t power_off_data[] = {0x04, 0xc2, 0x00, 0x00, 0xA2};
        power_off_data[2] = ble_send_data.slave_id[0]; power_off_data[3] = ble_send_data.slave_id[1];
        UartWrite(WIFI_IOT_UART_IDX_2, power_off_data, sizeof(power_off_data));
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_send_data.ble_send_user_cmd_state == CMD_ING)
	{
		if(ble_receive_data.ble_time_out < BLE_CTRL_TIME_MAX)
		{
			ble_receive_data.ble_time_out++; 
		}
		else    //已超时
		{
            ble_send_data.ble_send_user_cmd_state = CMD_TIMEOUT;
            if(terminal_send_data.send_power_off_result_flag == 1)
            {
                terminal_send_data.send_power_off_result_flag = 4;
            }
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
		}
	}
}

static void ble_send_power_on(void)
{
    if(ble_send_data.ble_send_user_cmd_state != CMD_ING)
    {
        ble_send_data.ble_send_user_cmd_state = CMD_ING;
        uint8_t power_on_data[] = {0x04, 0xc2, 0x00, 0x00, 0xA3};
        power_on_data[2] = ble_send_data.slave_id[0]; power_on_data[3] = ble_send_data.slave_id[1];
        UartWrite(WIFI_IOT_UART_IDX_2, power_on_data, sizeof(power_on_data));
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_send_data.ble_send_user_cmd_state == CMD_ING)
	{
		if(ble_receive_data.ble_time_out < BLE_CTRL_TIME_MAX)
		{
			ble_receive_data.ble_time_out++; 
		}
		else    //已超时
		{
            ble_send_data.ble_send_user_cmd_state = CMD_TIMEOUT;
            if(terminal_send_data.send_power_on_result_flag == 1)
            {
                terminal_send_data.send_power_on_result_flag = 4;
            }
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
		}
	}
}


static void ble_send_timing_off(void)
{
    if(ble_send_data.ble_send_user_cmd_state != CMD_ING)
    {
        ble_send_data.ble_send_user_cmd_state = CMD_ING;
        uint8_t send_buff[12] = {0x0B, 0xc2, 0x00, 0x00, 0xA6};
        send_buff[2] = ble_send_data.slave_id[0]; send_buff[3] = ble_send_data.slave_id[1];
        memcpy(send_buff+5, ble_send_data.ble_timing_data, 6);
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, 12);
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_send_data.ble_send_user_cmd_state == CMD_ING)
	{
		if(ble_receive_data.ble_time_out < BLE_CTRL_TIME_MAX)
		{
			ble_receive_data.ble_time_out++; 
		}
		else    //已超时
		{
            ble_send_data.ble_send_user_cmd_state = CMD_TIMEOUT;
            if(terminal_send_data.send_timing_off_result_flag == 1)
            {
                terminal_send_data.send_timing_off_result_flag = 4;
            }
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
		}
	}
}

static void ble_send_timing_on(void)
{
    if(ble_send_data.ble_send_user_cmd_state != CMD_ING)
    {
        ble_send_data.ble_send_user_cmd_state = CMD_ING;
        uint8_t send_buff[12] = {0x0B, 0xc2, 0x00, 0x00, 0xA7};
        send_buff[2] = ble_send_data.slave_id[0]; send_buff[3] = ble_send_data.slave_id[1];
        memcpy(send_buff+5, ble_send_data.ble_timing_data, 6);
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, 12);
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_send_data.ble_send_user_cmd_state == CMD_ING)
	{
		if(ble_receive_data.ble_time_out < BLE_CTRL_TIME_MAX)
		{
			ble_receive_data.ble_time_out++; 
		}
		else    //已超时
		{
            ble_send_data.ble_send_user_cmd_state = CMD_TIMEOUT;
            if(terminal_send_data.send_timing_off_result_flag == 1)
            {
                terminal_send_data.send_timing_off_result_flag = 4;
            }
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
		}
	}
}

static void ble_send_time_off(void)
{
    if(ble_send_data.ble_send_user_cmd_state != CMD_ING)
    {
        ble_send_data.ble_send_user_cmd_state = CMD_ING;
        uint8_t send_buff[12] = {0x0B, 0xc2, 0x00, 0x00, 0xA8};
        send_buff[2] = ble_send_data.slave_id[0]; send_buff[3] = ble_send_data.slave_id[1];
        memcpy(send_buff+5, ble_send_data.ble_time_data, 6);
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, 12);
        printf("%s\n", ble_send_data.ble_time_data);
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_send_data.ble_send_user_cmd_state == CMD_ING)
	{
		if(ble_receive_data.ble_time_out < BLE_CTRL_TIME_MAX)
		{
			ble_receive_data.ble_time_out++; 
		}
		else    //已超时
		{
            ble_send_data.ble_send_user_cmd_state = CMD_TIMEOUT;
            if(terminal_send_data.send_time_off_result_flag == 1)
            {
                terminal_send_data.send_time_off_result_flag = 4;
            }
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
		}
	}
}

static void ble_send_time_on(void)
{
    if(ble_send_data.ble_send_user_cmd_state != CMD_ING)
    {
        ble_send_data.ble_send_user_cmd_state = CMD_ING;
        uint8_t send_buff[12] = {0x0B, 0xc2, 0x00, 0x00, 0xA9};
        send_buff[2] = ble_send_data.slave_id[0]; send_buff[3] = ble_send_data.slave_id[1];
        memcpy(send_buff+5, ble_send_data.ble_time_data, 6);
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, 12);
        printf("%s\n", ble_send_data.ble_time_data);
        ble_receive_data.ble_time_out = 0;
    }
    else if(ble_send_data.ble_send_user_cmd_state == CMD_ING)
	{
		if(ble_receive_data.ble_time_out < BLE_CTRL_TIME_MAX)
		{
			ble_receive_data.ble_time_out++; 
		}
		else    //已超时
		{
            ble_send_data.ble_send_user_cmd_state = CMD_TIMEOUT;
            if(terminal_send_data.send_time_on_result_flag == 1)
            {
                terminal_send_data.send_time_on_result_flag = 4;
            }
			ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
		}
	}
}

/***************************************************************************************************************************************
 *                                                               发送时间信息
 * *************************************************************************************************************************************/
static void ble_send_time(void)
{
    uint8_t send_buff[15] = {0};
    if(server_data.server_connect_state == 1)
    {
        send_buff[0] = 0x0E; send_buff[1] = 0xc2; send_buff[2] = ble_send_data.slave_id[0]; send_buff[3] = ble_send_data.slave_id[1]; send_buff[4] = 0xA1;  //3+8=11//A1主机发送时间
        send_buff[5] = 0x00; memcpy(send_buff+6, device_data.stime, 9);  //0x00时间正常
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, 15);
    }
    else
    {
        send_buff[0] = 0x05; send_buff[1] = 0xc2; send_buff[2] = ble_send_data.slave_id[0]; send_buff[3] = ble_send_data.slave_id[1]; send_buff[4] = 0xA1;
        send_buff[5] = 0xEE;    //0xEE未连接服务器
        UartWrite(WIFI_IOT_UART_IDX_2, send_buff, 6);
    }
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}


/***************************************************************************************************************************************
 *                                                               回应从机连接请求
 * *************************************************************************************************************************************/
static void ble_send_connect_replay(void)
{
    uint8_t access_succeed[] = {0x04, 0xC2, 0x00, 0x02, 0xA0};
    access_succeed[2] = ble_receive_data.ble_slave_id_data[ble_receive_data.ble_num-1].ble_slave_id[0];
    access_succeed[3] = ble_receive_data.ble_slave_id_data[ble_receive_data.ble_num-1].ble_slave_id[1];
    UartWrite(WIFI_IOT_UART_IDX_2, access_succeed, sizeof(access_succeed));
    ble_send_data.ble_send_cmd_num = BLE_CMD_MIN;
}
